package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

public class FoodDaoImpl implements FoodDao{
	
	Connection con = null;
	PreparedStatement pst = null;
	ResultSet rs = null;

	@Override
	public List<Food> listFoodByBusinessId(Integer businessId) {
		List<Food> list = new ArrayList<Food>();
		String sql = "SELECT * FROM food WHERE businessId = ? ";
		
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,businessId);
			rs = pst.executeQuery();
			while(rs.next()){
				Food food = new Food();
				food.setFoodId(rs.getInt("foodId"));
				food.setBusinessId(rs.getInt("businessId"));
				food.setFoodExplain(rs.getString("foodExplain"));
				food.setFoodName(rs.getString("foodName"));
				food.setFoodPrice(rs.getDouble("foodPrice"));
				//将数据添加到集合
				list.add(food);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, pst, con);
		}
		
		return list;
	}

	@Override
	public int saveFood(Food food) {
		int result = 0;
		String sql = "INSERT INTO food  VALUES (null,?,?,?,?) ";
		
		
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1,food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setDouble(3,food.getFoodPrice());
			pst.setInt(4,food.getBusinessId());
			
			//java中对数据库有影响的操作，均用Update，只有查询用Query。
			result = pst.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, pst, con);
		}
		
		return result;
	}
	
	@Override
	public Food getFoodById(Integer foodId) { 
		Food food = null;
		String sql = "SELECT * FROM food WHERE foodId=? ";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, foodId);
			rs = pst.executeQuery();
        while(rs.next()) {
            food = new Food();
            food.setFoodId(rs.getInt("foodId"));
            food.setFoodName(rs.getString("foodName"));
            food.setFoodExplain(rs.getString("foodExplain"));
            food.setFoodPrice(rs.getDouble("foodPrice"));
            food.setBusinessId(rs.getInt("businessId"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(rs, pst, con);
    }
    return food;
}

	@Override
	public int updateFood(Food food) {
		int result = 0;
		String sql = "UPDATE food SET  foodName = ?,foodExplain = ?,foodPrice = ? WHERE foodId = ? ";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1,food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setDouble(3,food.getFoodPrice());
			pst.setInt(4,food.getFoodId());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, pst, con);
		}
		return result;
	}

	@Override
	public int removeFood(Integer foodId) {
		int result = 0;
		String sql = "DELETE FROM food WHERE foodId = ? ";
		
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, foodId);
			result = pst.executeUpdate();
		} catch (SQLException e) {
				e.printStackTrace();
		}finally{
			DBUtil.close(rs, pst, con);
		}
		return result;
	}

	
	

}
